<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';
include("FCKeditor/fckeditor.php") ;
require_once './include/advisory_db.php';
//print_r($_SESSION);
$smarty->compile_check = true;
//accessblock(array("Trial"));

$jid   = trim($_GET[jid]) + 0;
$smarty->assign("view",$_GET[view]);
$smarty->assign('jid',$jid);

$sql    = "SELECT * FROM pa_job WHERE jo_id = '".$jid."'";
//echo $sql;
$jobarr = $DB->fetch_one_array($sql);
if($_SESSION[usertype] != "recruiter" || $jobarr[jo_uid]==$_SESSION[uid] || $jobarr['jo_status'] != 1 ){
   $err = "Invalid ID! 1. It's your posted job. 2. Job status is Closed or On Hold";
   $smarty->display('header.tpl');
   $smarty->assign("err",$err);
   $smarty->display("left.tpl");
   $smarty->display("error.tpl");
   $smarty->display("right.tpl");
   $smarty->display('footer.tpl');
   exit;
}

//recruiter Info
if($jobarr['jo_utype'] == 'recruiter'){
	$sql = "SELECT * FROM pa_recuiter WHERE rc_id = '".$jobarr['jo_uid']."' ";
	$recInfo = $DB->fetch_one_array($sql);
}
//jquery
$smarty->assign('jquery',1);
$smarty->assign('tokeninput',1);

$smarty->display('header.tpl');
$smarty->assign("date",date("m-d-Y"));
$smarty->assign("from",$_SESSION['rc_copname']."[".$_SESSION['useremail']."]");

if($_GET['action']=="send" || $_GET['action'] == "resend" ){
	  
	  $err = '';
	  if($_GET['action'] == "send"){//send
		  if(trim($_POST['e_subject'])==''){
			  $err .= "Subject is required<br>";
		  }		  
	  }else{//resend
		 $id = $_GET['id']+0; 
		 if($id <= 1) {
			echo "<meta http-equiv=\"refresh\" content=\"0;URL=errorall.php?errid=rc_1012\">";
			exit;
		 }
		 $sql = "SELECT * FROM pa_email WHERE e_sentrcid = '".$_SESSION['uid']."' AND e_id = '".$id."' ";
		 $eInfo = $DB->fetch_one_array($sql);
	  }
	  if($err == ''){
			$to_email = $recInfo['rc_email'];
			//$to_name = $recInfo['rc_fname']. " ". $recInfo['rc_lname'];
			$to_name = $recInfo['rc_copname'];
			$content = $_POST['content'] ;
	  }

	  if($err == ''){
			if(trim($_POST['preview']) == "Preview"){//Preview
				$emailInfo['e_subject'] = $_POST['e_subject'];
				$emailInfo['e_fromemail'] = $_SESSION['useremail'];
				$emailInfo['e_content'] = $content;
			
				
				//$emailInfo['e_toemail'] = $to_email."(".$to_name.")";
				$emailInfo['e_toemail'] = $to_name;
					
				if($jobInfo == ''){$jobInfo = 'N/A';}
				$emailInfo['attachJobs'] = $jobInfo;
				$emailInfo['date'] = date("m-d-Y");
				$emailInfo['realname'] = $_SESSION['rc_copname'];
				$smarty->assign("s",$emailInfo);


				$smarty->display('allleft.tpl');
				$smarty->display('previewsendmessage.tpl');
				$smarty->display('footer.tpl');
				exit;
			}

			$sql = "insert into " . $db_prefix . "contact (re_uid,re_aliasid,re_usertype,re_uname,re_regtime,re_updtime) values ('".$_SESSION[uid]."','".$jid."','".$_SESSION[usertype]."','".$_SESSION[uname]."','".time()."','".time()."')";
			$DB->query($sql);
			$insertid = $DB->insert_id();

			//sender's detail
			$sql = "SELECT * FROM pa_recuiter WHERE rc_id = '".$_SESSION[uid]."' ";
			$senderRecInfo = $DB->fetch_one_array($sql);

			//update the contact table
			$sql = "UPDATE " . $db_prefix . "contact 
							 SET 
							`re_uname`='".$_SESSION[uname]."',
							`re_fname`='" . $senderRecInfo[rc_fname]. "'  ,
							`re_lname`='" . $senderRecInfo[rc_lname]. "'  ,
							`re_email`='" . $senderRecInfo[rc_email]. "' ,
							`re_address1`='" . $senderRecInfo[rc_address1]. "' ,
							`re_address2`='" . $senderRecInfo[rc_address2]. "' , 
							`re_subject`='" . $_POST['e_subject'] . "' ,
							`re_detail`='" . $content . "' ,
							`re_updtime`='".time()."'

							 WHERE re_id = '".$insertid."'" ;
			$DB->query($sql);

			//update the message table
			if($_SESSION[usertype] == 'recruiter' ){
				$type = 1;

				$sql = "INSERT INTO pa_email (e_sentrcid, e_receivedrcid, e_sendertype,e_receivertype,e_joid, e_type, e_fromemail, e_fromname, e_toemail, e_toname, e_subject, e_content,e_contactid, e_created) VALUES (
					'".$_SESSION['uid']."',
					'".$recInfo['rc_id']."',
					'recruiter',
					'recruiter',
					'".$jid."',
					'".$type."',
					'".$_SESSION['useremail']."',
					'".$_SESSION['rc_copname']." (".$_SESSION['realname'].")',
					'".$recInfo['rc_email']."',
					'".$recInfo['rc_copname']."',
					'". $_POST['e_subject'] ."',
					'".$content."',
					'".$insertid."',
					'".time()."'
				)";
				$DB->query($sql);
			}

			//Send Email
			require_once("./include/class.phpmailer.php");
			$mail=new PHPMailer();

			$mail->IsMail();
			$mail->From     = $_SESSION['useremail'];
			$mail->FromName = $_SESSION['realname'];
			$mail->AddAddress($to_email,$to_name);
			

			$mail->IsHTML(true);
			$mail->Subject = $_POST['e_subject'];
			$mail->Body = "Message Forwarded from <a href='www.enetrecruiter.com'>enetRecruiter.com</a> <br><br>".
			stripslashes($content);

			$mail->CharSet = 'UTF-8';

			$mail->Send();
			$err  = "<h3 style=\"color:red\">Sent Successfully!</h3><meta http-equiv=\"refresh\" content=\"2;URL=mycontactjobs.php\">";
	  }
}

//all my jobs
$sql = "SELECT jo_id,jo_title,jo_city,jo_state FROM pa_job WHERE jo_uid = '".$_SESSION['uid']."' AND jo_utype='recruiter' AND jo_status <> '3' ORDER BY jo_id DESC";
$tp_jobs = $DB->fetch_all($sql);
if(!empty($tp_jobs)){
	foreach($tp_jobs as $job){
		$jobs[] = '{"name":"=>'.$job['jo_title'].' ('.$job['jo_id'].')","id":"'.$job['jo_id'].'"}';
	}
}
$jobOptions = @implode(",",$jobs);
$jobOptions = str_replace("++","+",$jobOptions);

//FCKeditor
$oFCKeditor = new FCKeditor("content") ;
$oFCKeditor->BasePath	= "FCKeditor/" ;
$oFCKeditor->Value = '' ;
$oFCKeditor->Width = 500;
$oFCKeditor->Height =300;
$oFCKeditor->ToolbarSet = "Basic";
$oFCKeditor->Value = "";

$smarty->assign("fckcontent",$oFCKeditor->CreateHtml());


if($err!=''){
$smarty->assign("err",$err);
}
//$smarty->assign("myjobs",$jobOptions);
$smarty->assign("pfsubject","Interested in your job: ".$jobarr['jo_title'].", Req#".$jid);
$smarty->assign("pfsendto","<b>".$recInfo['rc_copname']."</b>");
$smarty->assign("resumeid",$id);
$smarty->display('allleft.tpl');
$smarty->display('recruitercontact.tpl');
$smarty->display('footer.tpl');

?>